/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SamiIDUN.java
 *
 * Created on 25-oct-2011, 23:03:48
 */
package Comun;

import Controlador.Controladores.FuenteTabla;
import Controlador.Interfaz.DataFuente;
import Controlador.Interfaz.GlassPanel;
import javax.swing.JFrame;
import javax.swing.SwingWorker;


public class SamiIDUN extends javax.swing.JDialog {
    private FuenteTabla FT=null;
    private pnlNavegacion nav=null;
    private String tabla="";
    private boolean esNuevo=false;
    private JFrame parent;
    /** Creates new form SamiIDUN */
    public SamiIDUN(JFrame parent, boolean modal,FuenteTabla FT, String tabla, pnlNavegacion nav ) {
        super(parent, modal);
        initComponents();
        this.setGlassPane(new GlassPanel());
        this.parent=parent;
        this.FT=FT;
        this.nav=nav;
        this.tabla=tabla;
        this.setLocation(parent.getX(), parent.getHeight()+parent.getY()+10);
    }
    
    public boolean isTodoOK(){
        boolean resultado =true;
        for (DataFuente object : this.FT.getLstObservadores())  resultado=resultado && object.validar();
        return resultado; 
    }

    private void Limpiar(){
         for (DataFuente object : this.FT.getLstObservadores()) object.Limpiar();
    }
 
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panel1 = new org.edisoncor.gui.panel.Panel();
        samiButton2 = new Comun.SamiButton();
        samiButton3 = new Comun.SamiButton();
        samiButton4 = new Comun.SamiButton();
        samiButton5 = new Comun.SamiButton();
        samiButton1 = new Comun.SamiButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setUndecorated(true);
        getContentPane().setLayout(new java.awt.GridLayout(1, 0));

        panel1.setColorPrimario(new java.awt.Color(0, 102, 204));

        samiButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_plus 2.png"))); // NOI18N
        samiButton2.setMouseEntered(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_plus.png"))); // NOI18N
        samiButton2.setMouseExited(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_plus 2.png"))); // NOI18N
        samiButton2.setMousePressed(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_plus.png"))); // NOI18N
        samiButton2.setMouseReleased(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_plus.png"))); // NOI18N
        samiButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                samiButton2MouseClicked(evt);
            }
        });
        panel1.add(samiButton2);

        samiButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_folder 2.png"))); // NOI18N
        samiButton3.setMouseEntered(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_folder.png"))); // NOI18N
        samiButton3.setMouseExited(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_folder 2.png"))); // NOI18N
        samiButton3.setMousePressed(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_folder.png"))); // NOI18N
        samiButton3.setMouseReleased(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_folder.png"))); // NOI18N
        samiButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                samiButton3MouseClicked(evt);
            }
        });
        panel1.add(samiButton3);

        samiButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_dont 2.png"))); // NOI18N
        samiButton4.setMouseEntered(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_dont.png"))); // NOI18N
        samiButton4.setMouseExited(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_dont 2.png"))); // NOI18N
        samiButton4.setMousePressed(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_dont.png"))); // NOI18N
        samiButton4.setMouseReleased(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_dont.png"))); // NOI18N
        samiButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                samiButton4MouseClicked(evt);
            }
        });
        panel1.add(samiButton4);

        samiButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_less 2.png"))); // NOI18N
        samiButton5.setMouseEntered(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_less.png"))); // NOI18N
        samiButton5.setMouseExited(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_less 2.png"))); // NOI18N
        samiButton5.setMousePressed(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_less.png"))); // NOI18N
        samiButton5.setMouseReleased(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_less.png"))); // NOI18N
        samiButton5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                samiButton5MouseClicked(evt);
            }
        });
        panel1.add(samiButton5);

        samiButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_search 2.png"))); // NOI18N
        samiButton1.setMouseEntered(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_search.png"))); // NOI18N
        samiButton1.setMouseExited(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_search 2.png"))); // NOI18N
        samiButton1.setMousePressed(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_search.png"))); // NOI18N
        samiButton1.setMouseReleased(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_search.png"))); // NOI18N
        samiButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                samiButton1MouseClicked(evt);
            }
        });
        panel1.add(samiButton1);

        getContentPane().add(panel1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void samiButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_samiButton2MouseClicked
        //Nuevo
        this.estadoEdicion();
        this.Limpiar();
        this.esNuevo=true;
    }//GEN-LAST:event_samiButton2MouseClicked

    private void samiButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_samiButton3MouseClicked
        //Guardar
         final SwingWorker worker = new SwingWorker() {
           
           @Override
           protected Object doInBackground() throws Exception {
            parent.getGlassPane().setVisible(true);
            getGlassPane().setVisible(true);
            if(esNuevo && isTodoOK()){
            if(FT.Guardar(tabla)){ 
                estadoNormal();
                esNuevo=false;
                new Error(parent, true, "Registro Guardado Correctamente", 0, "Guardar").setVisible(true);
            }else{
                new Error(parent, true, "No se puede Almacenar el Registro", 0, "Error al Guardar").setVisible(true);
            }
        }
        else if(isTodoOK()){
            estadoNormal();
        }
            
            parent.getGlassPane().setVisible(false);
            getGlassPane().setVisible(false);
            return null;
            }
        };
         
       worker.execute();
        
        
        
    }//GEN-LAST:event_samiButton3MouseClicked

    private void samiButton4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_samiButton4MouseClicked
        //cancelar
        final SwingWorker worker = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {
                 estadoNormal();
                 return null;
            }
        };
       worker.execute();
    }//GEN-LAST:event_samiButton4MouseClicked
    public void estadoNormal(){
            this.nav.setVisible(true);
            this.samiButton1.setEnabled(true);
            this.samiButton2.setEnabled(true);
            this.samiButton4.setEnabled(true);
            this.samiButton5.setEnabled(true);
            this.FT.Refrescar();
    
    }
    
    private void estadoEdicion(){
        this.nav.setVisible(false);
        this.samiButton1.setEnabled(false);
        this.samiButton2.setEnabled(false);
        this.samiButton5.setEnabled(false);
    
    }
    private void samiButton5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_samiButton5MouseClicked
        // Eliminar
        final SwingWorker worker = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {
                
                String res= new Error(parent, true, "¿Desea Eliminar el Registro Seleccionado?", 1, "Eliminar").activar();
                parent.getGlassPane().setVisible(true);
                getGlassPane().setVisible(true);
                if(res.equals("1"))
                    if(!FT.Eliminar(tabla)) new Error(parent, true, "No se puede Eliminar el Registro", 0, "Error al Guardar").setVisible(true);
                     else
                    estadoNormal();
                
                parent.getGlassPane().setVisible(false);
                getGlassPane().setVisible(false);
                return null;
            }
        };
        
        worker.execute();
        
        
    }//GEN-LAST:event_samiButton5MouseClicked

    private void samiButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_samiButton1MouseClicked
        // Buscar
    }//GEN-LAST:event_samiButton1MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SamiIDUN.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SamiIDUN.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SamiIDUN.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SamiIDUN.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                SamiIDUN dialog = new SamiIDUN(new javax.swing.JFrame(), true,null,"",null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.edisoncor.gui.panel.Panel panel1;
    private Comun.SamiButton samiButton1;
    private Comun.SamiButton samiButton2;
    private Comun.SamiButton samiButton3;
    private Comun.SamiButton samiButton4;
    private Comun.SamiButton samiButton5;
    // End of variables declaration//GEN-END:variables
}
